package com.ruoyi.web.controller.gcgl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcQtfksqd;
import com.ruoyi.gcgl.domain.GcQtfksqditem;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcQtfksqdService;
import com.ruoyi.gcgl.service.IGcQtfksqditemService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.web.controller.tool.AliyunSmsUtils;

/**
 * 中标服务费Controller
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Controller
@RequestMapping("/gcgl/qtfksqd")
public class GcQtfksqdController extends BaseController
{
    private String prefix = "gcgl/qtfksqd";

    @Autowired
    private IGcQtfksqdService gcQtfksqdService;
    @Autowired
    private IGcQtfksqditemService gcQtfksqditemService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private IGcSupplyService gcSupplyService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;

    @RequiresPermissions("gcgl:qtfksqd:view")
    @GetMapping()
    public String qtfksqd(ModelMap mmap)
    {
        return prefix + "/qtfksqd";
    }

    /**
     * 查询其它付款申请单列表
     */
    @RequiresPermissions("gcgl:qtfksqd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcQtfksqd gcQtfksqd)
    {
        startPage();
        gcQtfksqd.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        gcQtfksqd.setCreateUser(getLoginName());
        }
        List<GcQtfksqd> list = gcQtfksqdService.selectGcQtfksqdList(gcQtfksqd);
        return getDataTable(list);
    }

    /**
     * 导出其它付款申请单列表
     */
    @RequiresPermissions("gcgl:qtfksqd:export")
    @Log(title = "中标服务费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcQtfksqd gcQtfksqd)
    {
    	gcQtfksqd.setDeleteFlag(1);
        if(getSysUser().isShow()){
        gcQtfksqd.setCreateUser(getLoginName());
        }
        List<GcQtfksqd> list = gcQtfksqdService.selectGcQtfksqdList(gcQtfksqd);
        ExcelUtil<GcQtfksqd> util = new ExcelUtil<GcQtfksqd>(GcQtfksqd.class);
        return util.exportExcel(list, "中标服务费数据");
    }

    /**
     * 新增中标服务费
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("fksqr", getLoginName());
    	
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	gcSupply.setType(5);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
        return prefix + "/add";
    }

    /**
     * 新增保存中标服务费
     */
    @RequiresPermissions("gcgl:qtfksqd:add")
    @Log(title = "中标服务费", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcQtfksqd gcQtfksqd){
    	try {
    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
    		int year = Integer.parseInt(sdy.format(new Date()));
    		int month = Integer.parseInt(sdm.format(new Date()));
    		GcQtfksqd newObj = new GcQtfksqd();
    		newObj.setDeleteFlag(1);
    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, month));
    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, month));
    		List<GcQtfksqd> lisCount = gcQtfksqdService.selectGcQtfksqdList(newObj);
    		
        	GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setType(5);
        	gcSupply.setName(gcQtfksqd.getSkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        	
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcQtfksqd.setSkdw(gcSupplylis.get(0).getName());
            	gcQtfksqd.setSkzh(gcSupplylis.get(0).getSkzh());
            	gcQtfksqd.setNsrsbh(gcSupplylis.get(0).getNssbh());
            	gcQtfksqd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
        	
    		String no = String.format("%02d", lisCount.size()+1);
    		String l_month = String.format("%02d", month);
    		gcQtfksqd.setOrderNo(year+"-"+l_month+"-"+no);
    		gcQtfksqd.setDeleteFlag(1);
    		gcQtfksqd.setCreateAt(new Date());
    		gcQtfksqd.setUpdateAt(new Date());
    		gcQtfksqd.setCreateUser(getLoginName());
    		gcQtfksqd.setUpdateUser(getLoginName());
    		gcQtfksqd.setSqr(getLoginName());
    		gcQtfksqd.setSqrAt(new Date());
    		gcQtfksqd.setStatus(0);
    		
//    		GcProject gcProject = gcProjectService.selectGcProjectById(Long.parseLong(gcQtfksqd.getGcProjectId().toString()));
//    		gcQtfksqd.setGcProjectName(gcProject.getName());
    		
    		gcQtfksqdService.insertGcQtfksqd(gcQtfksqd);
    		
    		Double hejexx = 0.00;
        	for(GcQtfksqditem obj:gcQtfksqd.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(obj.getJine().toString()));
        		obj.setDeleteFlag(1);
        		obj.setCreateAt(new Date());
        		obj.setUpdateAt(new Date());
        		obj.setCreateUser(getLoginName());
        		obj.setUpdateUser(getLoginName());
        		obj.setQtfksqdId(gcQtfksqd.getId());
        		gcQtfksqditemService.insertGcQtfksqditem(obj);
        	}

			DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
        	gcQtfksqd.setHejexx(mf.format(hejexx));
        	gcQtfksqd.setHeje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcQtfksqdService.updateGcQtfksqd(gcQtfksqd);
        	
        	//发送消息通知
//        	gcSendTipService.sendTipXm("qtfk", "中标服务费", gcQtfksqd.getOrderNo(), 
//        			gcQtfksqd.getCreateUser(), DateUtils.dateTime(gcQtfksqd.getCreateAt()), 
//        			gcQtfksqd.getStatus().toString(), null, gcQtfksqd.getId().toString());
        	
        	try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQtfksqd.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        		String phonenumber = caiw;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 修改中标服务费
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcQtfksqd gcQtfksqd = gcQtfksqdService.selectGcQtfksqdById(id);
        GcQtfksqditem gcQtfksqditem = new GcQtfksqditem();
        gcQtfksqditem.setQtfksqdId(id);
        List<GcQtfksqditem> items = gcQtfksqditemService.selectGcQtfksqditemList(gcQtfksqditem);
        gcQtfksqd.setItem(items);
        mmap.put("gcQtfksqd", gcQtfksqd);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	gcSupply.setType(5);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
        return prefix + "/edit";
    }

    /**
     * 修改保存中标服务费
     */
    @Log(title = "中标服务费", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcQtfksqd gcQtfksqd)
    {
    	try {
    		gcQtfksqditemService.deleteGcQtfksqditemById(gcQtfksqd.getId());
    		
    		Double hejexx = 0.00;
        	//item
        	for(GcQtfksqditem item : gcQtfksqd.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(item.getJine().toString()));
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setQtfksqdId(gcQtfksqd.getId());
        		gcQtfksqditemService.insertGcQtfksqditem(item);
        	}
        	
        	GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setType(5);
        	gcSupply.setName(gcQtfksqd.getSkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        	
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcQtfksqd.setSkdw(gcSupplylis.get(0).getName());
            	gcQtfksqd.setSkzh(gcSupplylis.get(0).getSkzh());
            	gcQtfksqd.setNsrsbh(gcSupplylis.get(0).getNssbh());
            	gcQtfksqd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
    		
    		DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
        	gcQtfksqd.setHejexx(mf.format(hejexx));
        	gcQtfksqd.setHeje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcQtfksqd.setUpdateAt(new Date());
        	gcQtfksqd.setUpdateUser(getLoginName());
        	
//        	GcProject obj = gcProjectService.selectGcProjectById(Long.parseLong(gcQtfksqd.getGcProjectId().toString()));
//    		gcQtfksqd.setGcProjectName(obj.getName());
        	
        	gcQtfksqdService.updateGcQtfksqd(gcQtfksqd);
        	
        	try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQtfksqd.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        		String phonenumber = caiw;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
        return toAjax(1);
    }

    /**
     * 删除中标服务费
     */
    @RequiresPermissions("gcgl:qtfksqd:remove")
    @Log(title = "中标服务费", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcQtfksqdService.deleteGcQtfksqdByIds(ids));
    }
    
    /////财务审批
    @RequiresPermissions("gcgl:qtfksqd:cwpsview")
    @GetMapping("/cwps")
    public String cwps()
    {
        return prefix + "/qtfksqdcwps";
    }
    
    @PostMapping("/cwpslist")
    @ResponseBody
    public TableDataInfo cwpslist(GcQtfksqd gcQtfksqd)
    {
        startPage();
        gcQtfksqd.setDeleteFlag(1);
        gcQtfksqd.setStatus(1);
        List<GcQtfksqd> list = gcQtfksqdService.selectGcQtfksqdList(gcQtfksqd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwyes")
    @ResponseBody
    public AjaxResult cwyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQtfksqd obj = new GcQtfksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setCwsh(getLoginName());
    		obj.setCwshAt(new Date());
    		gcQtfksqdService.updateGcQtfksqd(obj);
    		
    		GcQtfksqd gcQtfksqd = gcQtfksqdService.selectGcQtfksqdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTipXm("qtfk", "中标服务费", gcQtfksqd.getOrderNo(), 
//        			gcQtfksqd.getCreateUser(), DateUtils.dateTime(gcQtfksqd.getCreateAt()), 
//        			gcQtfksqd.getStatus().toString(), null, gcQtfksqd.getId().toString());
    		
    		try {
//        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQtfksqd.getCreateUser());
//        		if(userlis!=null && userlis.size()>0){
//        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
    			String phonenumber = zjl;
        			AliyunSmsUtils.returnSmsTz(phonenumber);
//        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwno")
    @ResponseBody
    public AjaxResult cwno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQtfksqd obj = new GcQtfksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setCwsh(getLoginName());
    		obj.setCwshAt(new Date());
    		gcQtfksqdService.updateGcQtfksqd(obj);
    		
    		GcQtfksqd gcQtfksqd = gcQtfksqdService.selectGcQtfksqdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTipXm("qtfk", "中标服务费", gcQtfksqd.getOrderNo(), 
//        			gcQtfksqd.getCreateUser(), DateUtils.dateTime(gcQtfksqd.getCreateAt()), 
//        			gcQtfksqd.getStatus().toString(), null, gcQtfksqd.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQtfksqd.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:qtfksqd:zjlpsview")
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/qtfksqdzjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcQtfksqd gcQtfksqd)
    {
        startPage();
        gcQtfksqd.setDeleteFlag(1);
        gcQtfksqd.setStatus(0);
        List<GcQtfksqd> list = gcQtfksqdService.selectGcQtfksqdList(gcQtfksqd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQtfksqd obj = new GcQtfksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(1);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcQtfksqdService.updateGcQtfksqd(obj);
    		
    		GcQtfksqd gcQtfksqd = gcQtfksqdService.selectGcQtfksqdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTipXm("qtfk", "中标服务费", gcQtfksqd.getOrderNo(), 
//        			gcQtfksqd.getCreateUser(), DateUtils.dateTime(gcQtfksqd.getCreateAt()), 
//        			gcQtfksqd.getStatus().toString(), null, gcQtfksqd.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQtfksqd.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcQtfksqd obj = new GcQtfksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(2);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcQtfksqdService.updateGcQtfksqd(obj);
    		
    		GcQtfksqd gcQtfksqd = gcQtfksqdService.selectGcQtfksqdById(Long.parseLong(id[i]));
    		//发送消息通知
//        	gcSendTipService.sendTipXm("qtfk", "中标服务费", gcQtfksqd.getOrderNo(), 
//        			gcQtfksqd.getCreateUser(), DateUtils.dateTime(gcQtfksqd.getCreateAt()), 
//        			gcQtfksqd.getStatus().toString(), null, gcQtfksqd.getId().toString());
    		
    		try {
        		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcQtfksqd.getCreateUser());
        		if(userlis!=null && userlis.size()>0){
        			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			AliyunSmsUtils.returnSmsTz(phonenumber);
        		}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/qtfksqdprint/{id}")
    public String qtfksqdprint(@PathVariable("id") Long id, ModelMap mmap){
    	GcQtfksqd obj = gcQtfksqdService.selectGcQtfksqdById(id);
        
    	GcQtfksqditem items = new GcQtfksqditem();
        items.setQtfksqdId(id);
        List<GcQtfksqditem> itemlis= gcQtfksqditemService.selectGcQtfksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/qtfksqdprint";
    }
    
    @GetMapping("/qtfksqdcwsp/{id}")
    public String qtfksqdcwsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcQtfksqd obj = gcQtfksqdService.selectGcQtfksqdById(id);
        
    	GcQtfksqditem items = new GcQtfksqditem();
        items.setQtfksqdId(id);
        List<GcQtfksqditem> itemlis= gcQtfksqditemService.selectGcQtfksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/qtfksqdcwsp";
    }
    
    @GetMapping("/qtfksqdzjlsp/{id}")
    public String qtfksqdzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcQtfksqd obj = gcQtfksqdService.selectGcQtfksqdById(id);
        
    	GcQtfksqditem items = new GcQtfksqditem();
        items.setQtfksqdId(id);
        List<GcQtfksqditem> itemlis= gcQtfksqditemService.selectGcQtfksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/qtfksqdzjlsp";
    }
}